package com.wltt.product.ahuthr.controller;

import com.wltt.product.ahuthr.model.ResultData;
import com.wltt.product.ahuthr.service.FileService;
import com.wltt.product.ahuthr.utils.FileUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping(value = "/api/file")
public class FileController {

    @Value("${file.template.path: ./template}")
    private String templatePath;

    @Resource
    private FileService fileService;

    /**
     * 下载公积金模板
     *
     * @return
     */
    @GetMapping(value = {"/download/houseFundTemplate"})
    public void houseFundTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        File file = new File(templatePath + "/HouseFundTemplate.xlsx");
        FileUtil.SendFile(response, file);
    }

    /**
     * 下载公积金模板
     *
     * @return
     */
    @GetMapping(value = {"/download/salaryLevelTemplate"})
    public void salaryLevelTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        File file = new File(templatePath + "/SalaryLevelTemplate.xlsx");
        FileUtil.SendFile(response, file);
    }

    /**
     * 上传
     *
     * @return
     */
    @PostMapping(value = {"/upload"})
    public ResultData upload(@RequestParam("file") MultipartFile file) throws IOException {
        return fileService.upload(file);
    }

    /**
     * 下载文件
     *
     * @return
     */
    @GetMapping(value = {"/get/image/{fileSaveName}"})
    public void download(HttpServletResponse response, @PathVariable("fileSaveName") String fileSaveName) throws IOException {
        fileService.getImage(response, fileSaveName);
    }
}
